const DatabasePool = require('./utils/dbPool.cjs');
const path = require('path');

async function checkData() {
  const dbPool = new DatabasePool(path.join(__dirname, 'data', 'pph.sqlite'));

  try {
    console.log('📊 检查数据库表结构和数据...');

    // 检查实际数据
    const recordsCount = await dbPool.query('SELECT COUNT(*) as count FROM pph_records');
    const targetsCount = await dbPool.query('SELECT COUNT(*) as count FROM pph_targets');
    const estimatesCount = await dbPool.query('SELECT COUNT(*) as count FROM pph_estimates');

    console.log('\n📊 数据统计:');
    console.log(`  - pph_records: ${recordsCount[0].count} 条记录`);
    console.log(`  - pph_targets: ${targetsCount[0].count} 条目标值`);
    console.log(`  - pph_estimates: ${estimatesCount[0].count} 条预估值`);

    // 检查具体数据样本
    const sampleRecords = await dbPool.query(`
      SELECT id, material_code, line_name, record_date, calculated_pph,
             target_pph, estimate_pph, efficiency_rate, average_pph,
             quantity, total_people, net_working_minutes, actual_takt_seconds,
             start_time, end_time, break_minutes
      FROM pph_records
      WHERE record_date = '2024-01-02'
      LIMIT 3
    `);

    console.log('\n📋 样本记录 (2024-01-02):');
    sampleRecords.forEach(record => {
      console.log(`  ID: ${record.id}`);
      console.log(`    物料: ${record.material_code} @ ${record.line_name}`);
      console.log(`    计算PPH: ${record.calculated_pph}`);
      console.log(`    目标PPH: ${record.target_pph}`);
      console.log(`    预估PPH: ${record.estimate_pph}`);
      console.log(`    达成率: ${record.efficiency_rate}%`);
      console.log(`    平均PPH: ${record.average_pph}`);
      console.log(`    数量: ${record.quantity}, 人数: ${record.total_people}`);
      console.log(`    时间: ${record.start_time} - ${record.end_time}, 休息: ${record.break_minutes}分钟`);
      console.log(`    净工作时间: ${record.net_working_minutes}分钟`);
      console.log(`    节拍时间: ${record.actual_takt_seconds}秒/件`);
      console.log('    ---');
    });

  } catch (error) {
    console.error('❌ 检查失败:', error.message);
  } finally {
    await dbPool.close();
  }
}

checkData();